package com.realife.eson.common.util.information;

import com.realife.eson.common.asserts.AssertNP;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/5/31 23:54
 * @description:
 */
@Slf4j
public class MeReflectUtil {
    private MeReflectUtil() {
    }


    /**
     * 反射获取对象字段: 值
     *
     * @param target
     * @return
     */
    public static Map<String, Object> getFieldKeyValue(Object target) {
        Map<String, Object> map = new HashMap<>();
        if (AssertNP.legalParams(target)) {
            Field[] fields = target.getClass().getDeclaredFields();
            if (AssertNP.okArray(fields)) {
                for (int i = 0; i < fields.length; i++) {
                    fields[i].setAccessible(true);
                    String key = fields[i].getName();
                    Object value = null;
                    try {
                        value = fields[i].get(target);
                    } catch (Exception e) {
                        log.info("[**======> {} **]", e.getMessage() + " >> " + e.getStackTrace()[0] + " >> " + e.getCause());
                    }
                    map.put(key, value);
                }
            }
        }
        return map;
    }


}

